const app = getApp();
Page({
  data: {
    currentTab: 0,
    tabItemsArray: [{
      type: "pet",
      name: "宠物"
    }, {
      type: "Idle",
      name: "闲置"
    }],
    pets: [],
    description: '',
    selectedPet: null,
    petIndex: null,
    title: "", //物品标题 
    price: "", //物品价格 
    descriptio: "", //物品描述
    imgs: [],
  },
  changeTabIndex: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
  },
  bindPetChange: function (e) {
    // console.log(e.detail.value);
    var selectedObject = this.data.pets[e.detail.value]
    console.log(selectedObject);
    this.setData({
      petIndex: e.detail.value,
      selectedPet: selectedObject
    })
  },
  onLoad(options) {
    var that = this;
    var openid = wx.getStorageSync('openid')
    wx.request({
      url: 'http://127.0.0.1:7001/api/user/petinfo',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'GET',
      data: {
        ownerid: openid
      },
      success: function (res) {
        console.log(res.data);
        const data = res.data.data.result
        // console.log(data);
        for (var i = 0; i < data.length; i++) {
          const {
            id,
            name,
            src
          } = data[i]
          const base64 = wx.arrayBufferToBase64(new Uint8Array(src.data));
          const image = 'data:image/png;base64,' + base64
          var pets = that.data.pets;
          pets.push({
            id: id,
            name: name,
            image: image
          })
          // console.log(pets);
          that.setData({
            pets: pets
          })
        }
      }
    })
  },
  onDescriptionChange(e) {
    this.setData({
      description: e.detail.value
    })
  },
  onSubmit() {
    var openid = wx.getStorageSync('openid')
    wx.request({
      url: 'http://127.0.0.1:7001/api/user/transfer',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      data: {
        ownerid: openid,
        petid: this.data.selectedPet.id,
        description: this.data.description,
        status: 0
      },
      success: function (res) {
        console.log(res.data);
        wx.redirectTo({
          url: '../home/home',
        })
      }
    })
  },
  inputTitle(e) {
    this.setData({
      title: e.detail.value
    })
  },
  inputPrice(e) {
    this.setData({
      price: e.detail.value
    })
  },
   //输入描述 
  inputDescription(e) {
    console.log(e.detail.value);
    this.setData({
      descriptio: e.detail.value
    })
  },
  chooseImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // console.log(res.tempFilePaths);
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        imgs.push(tempFilePaths[0]);
        that.setData({
          imgs: imgs
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  submitForm() { 
    let that = this; 
    let title = that.data.title; //获取标题 
    let price = that.data.price; //获取价格
    let description = that.data.descriptio; //获取描述
    var openid = wx.getStorageSync('openid')
    wx.request({
      url: 'http://127.0.0.1:7001/api/user/idle',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      data: {
        title: title,
        price: price,
        description: description,
        ownerid: openid,
        status:0,
      },
      success: function (res) {
        console.log(res.data);
        var id =  res.data.data.result.id
        wx.uploadFile({
          url: 'http://127.0.0.1:7001/api/user/idlepic', //接受图片的接口地址
          filePath: that.data.imgs[0],
          name: 'file',
          formData:{
            id:id 
          },
          success(res) {
            console.log(res.data);
            wx.redirectTo({
              url: '../home/home',
            })
            // console.log(JSON.parse(res.data).files[0].data);
            //do something
          }
        })
      },
      fail: function () {
        console.log('上传失败');
      }
    })
    
  }
})